//
//  ZQStatusDAL.swift
//  ZQWeibo
//
//  Created by summer的Dad on 16/11/26.
//  Copyright © 2016年 summer的Dad. All rights reserved.
//

import UIKit
//设置多少秒之后过期
private let expires_date_timeIntravel: Double = -7 * 24 * 60 * 60
class ZQStatusDAL: NSObject {

    
    class func loadHomeData(sinceId: Int64,maxId: Int64,finished: @escaping ([[String : Any]]?) -> ()) {
        
        let res = checkoutStatus(sinceId: sinceId, maxId: maxId)
        
         if let result = res, result.count > 0 {
            
            //缓存数据返回数据
            finished(result)
            
            return
      
        }
        
       let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let parameters = ["access_token" : ZQUserAccountViewModel.shared.access_token,
                          "max_id" : "\(maxId)",
            "since_id" : "\(sinceId)"]
        
        HMNetworkTools.shared.request(method: .GET, urlString: urlString, parameters: parameters, finished: { (responseObject,error) -> () in
            if error != nil {
                // 失败的回调
                //TODO: 完成失败的回调
                finished(nil)
                return
            }
            
            //成功
            let dict = responseObject as! [String : Any]
            //尝试转换为字典类型的数组
            guard let array = dict["statuses"] as? [[String : Any]] else {
                finished(nil)
                return
            }
            
            //回调的网络 请求的结果
            finished(array)
            //存储到本地
            cacheStatus(array: array)
        })
        
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    //1.检查本地是否有缓存数据
    class func checkoutStatus(sinceId: Int64,maxId: Int64) -> [[String : Any]]? {
        //查询的sql
        
        guard let userId = ZQUserAccountViewModel.shared.userAccount?.uid else {
            return nil
        }
        
        //会将数据库中所有的缓存数据都查找到
        var sql = "SELECT status FROM T_Status WHERE userId = \(userId) "
        //拼接sql
        //如果sinceId > 0 标识需要查找新数据
        if sinceId > 0 {
            //向上查找
            sql += "AND statusId > \(sinceId) "
        }
        
        if maxId > 0 {
            //向下查找
            sql += "AND statusId < \(maxId) "
        }
        
        //排序 降序
        sql += "ORDER BY statusId DESC "
        
        //限制条数
        sql += "LIMIT 100"
        
        print(sql)
        //实例化数据
        var array = [[String : Any]]()
        
        ZQSQLiteTools.shared.queue.inDatabase { (db) in
            guard let res = db!.executeQuery(sql, withArgumentsIn: nil) else {
                //在闭包中不能够return
                return
            }
            //逐条获取数据
            while res.next() {
                //获取status 二进制数据
                let jsonData = res.data(forColumn: "status")
                //转换为json 字典 外界用的都是字典嵌套模型
                let dict = try! JSONSerialization.jsonObject(with:jsonData!, options: [])
                array.append(dict as! [String : Any])
            }
        }
        
        return array
    }
  
   
    //获取网络数据,存储数据
    
    class func cacheStatus(array : [[String : Any]]) {
        
        //userID
       guard let userId = ZQUserAccountViewModel.shared.userAccount?.uid else{
            
            print("用户没有登录")
            return
        }
        
        
        let sql = "INSERT OR REPLACE INTO T_Status (statusId,status,userId) VALUES (?,?,?)"
        ZQSQLiteTools.shared.queue.inTransaction { (db, rollback) in

                for item in array {
                    let statusId = item["id"]!
                    //数据库中能够存储的数据不能够是Foundation 需要将字典数据转换二进制数据存储到数据库中
                    //如果是字典数据 在读取的时候会有问题
                    let jsonData = try! JSONSerialization.data(withJSONObject: item, options: [])
                    let res =  db!.executeUpdate(sql, withArgumentsIn: [statusId,jsonData,userId])
                    if !res {
                        //不成功就执行回滚
                        rollback?.pointee = true
                        return
                    }
       
            }

    }
   
  }
    
    //在什么时候清除 7天之前的微博
    class func clearCacheStatus() {
        //根据设置的过期描述 和当期日期 计算需要删除的微博记录
        let expires_date = Date(timeIntervalSinceNow: expires_date_timeIntravel)
        //yyyy-MM-dd HH:mm:ss
        let formater = DateFormatter()
        //设置本地化信息
        formater.locale = Locale(identifier: "en")
        //设置格式化符
        formater.dateFormat = "yyyy-MM-dd HH:mm:ss"
        //获取日期的字符串
        let dateStr = formater.string(from: expires_date)
        
        let sql = "DELETE FROM T_Status WHERE create_date < ?"
        //执行sql
        ZQSQLiteTools.shared.queue.inTransaction { (db, rollback) in
            let res = db!.executeUpdate(sql, withArgumentsIn: [dateStr])
            if !res {
                //执行回滚
                rollback?.pointee = true
            }
        }
    
    
    
}

}
